#!/usr/bin/perl -wc

package RFCMail::ESMTP::Server::base;

use strict;
use warnings;
use RFCMail::ESMTP::Object::ReversePath;
use RFCMail::ESMTP::Object::ForwardPath;
use RFCMail::ESMTP::Object::Parameters;
use RFCMail::ESMTP::Object::Mailbox;


sub ehlo_greet {
    my ($self) = @_;
    return ("greets ".$self->{state}->{Greeting});
}


sub ehlo_lines {
    my ($self) = @_;
    my @ehlo;
    push @ehlo, @{$self->{ehlo_lines}}
	if defined($self->{ehlo_lines});
    push @ehlo, map { $self->$_ } $self->{handlers}->ehlo_methods;
    return (@ehlo);
}


sub mail_from {
    my ($self, $from, $parm) = @_;
    return ([501, "Syntax error: invalid parameters"])
	if $parm->count;
    return ([250, "OK"]);
}


sub rcpt_to {
    my ($self, $to, $parm) = @_;
    return ([501, "Syntax error: invalid parameters"])
	if $parm->count;
    return ([250, "Postmaster OK"])
	if not $to->is_path;
    return ($self->local_mailbox($to));
}


sub rcpt_add {
    my ($self, $rcpt) = @_;
    my $state = $self->{state};
    $state->{recipients} = []
	if not defined($state->{recipients});
    # new recipients pushed only if they don't appear in the list already
    my $path = $rcpt->forward_path;
    push @{$state->{recipients}}, $path
	if not map { $path eq $_ ? 1 : () } $state->recipients;
}


sub vrfy_mailbox {
    my ($self, $mailbox) = @_;
    my $result = $self->local_mailbox($mailbox);
    $result->[0] = 252
	if $result->[0] =~ /^4/;
    return ($result);
}


sub local_mailbox { [250, "OK"] }


1;
